package com.bawei.park2211a.park

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bawei.base.bean.NoticeEntity
import com.bawei.park2211a.R
import com.bawei.park2211a.databinding.ItemNoticeBinding



class ParkAdapter : PagingDataAdapter<NoticeEntity, ParkAdapter.ParkViewHolder>(diff) {
    private var mOnItemClickListener: OnItemClickListener? = null
    companion object {
      // 定义一个变量diff，用于比较两个NoticeEntity对象是否相同
      // 这里使用了匿名对象的方式继承了ItemCallback类，并实现了其两个抽象方法
      var diff = object : ItemCallback<NoticeEntity>() {
          // 比较两个NoticeEntity对象的id是否相同
          // 用于判断两个对象是否代表同一个实体
          override fun areItemsTheSame(oldItem: NoticeEntity, newItem: NoticeEntity): Boolean {
              return oldItem.id == newItem.id
          }

          // 比较两个NoticeEntity对象的内容是否完全相同
          // 用于判断两个对象是否完全相等
          override fun areContentsTheSame(oldItem: NoticeEntity, newItem: NoticeEntity): Boolean {
              return oldItem == newItem
          }
      }
    }

    class ParkViewHolder(itemView: View) : ViewHolder(itemView) {
        val binding: ItemNoticeBinding? = DataBindingUtil.bind(itemView)
    }

    override fun onBindViewHolder(holder: ParkViewHolder, position: Int) {
        var item=getItem(position)!!
        holder.binding?.apply {
            noticeTime.text = item.publishTime
            noticeMsg.text=item.content
            noticeTitle.text=item.title

        }
        holder.itemView.setOnClickListener {
            mOnItemClickListener?.OnClickListener(position, item)
        }

    }
    fun setOnItemClickListener(listener: OnItemClickListener?) {
        this.mOnItemClickListener = listener
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ParkViewHolder {

        return ParkViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.item_notice, parent, false)
        )
    }
   interface OnItemClickListener{
       fun OnClickListener(position: Int,data:NoticeEntity)
   }

}